﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class index : System.Web.UI.Page
{
    RegistryKey RarKey;
    Object RarObj;
    string RarPath;
    protected void Page_Load(object sender, EventArgs e)
    {
        Regex regex = new Regex(@"[a-zA-Z]:[\\].*.[\\]");
        RarKey = Registry.ClassesRoot.OpenSubKey(rar_reg_key.Value);
        RarObj = RarKey.GetValue("");
        RarPath = RarObj.ToString();
        MsgInfo.Text = "RAR目录：" + regex.Match(RarPath).Value + "<br />" +
                    "网站目录：" + System.Web.HttpContext.Current.Server.MapPath("~") + "<br />" +
                    "文件上传目录：" + System.Web.HttpContext.Current.Server.MapPath("~/Content/Upload") + "<br />" +
                    "插件目录：" + System.Web.HttpContext.Current.Server.MapPath("~/Plugins") + "<br />" +
                    "解压目录：" + System.Web.HttpContext.Current.Server.MapPath("~/Content/UnpackTemp") + "<br />";
        XmlDocument cfgDoc = new XmlDocument();
        cfgDoc.Load(HttpContext.Current.Server.MapPath("~/web.config"));
        XmlNode node = cfgDoc.SelectSingleNode("//appSettings");
        if (node == null)
        {
            throw new InvalidOperationException("appSettings section not found");
        }
        try
        {
            // XPath select setting "add" element that contains this key       
            XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='dbconn']");
            if (addElem != null)
            {
                //ScriptManager.RegisterStartupScript(AjaxPanel, this.GetType(), "updateScript", "alert('此key已经存在！')", true);
            }
            // not found, so we need to add the element, key and value   
            else
            {
                XmlElement entry = cfgDoc.CreateElement("add");
                entry.SetAttribute("key", "dbconn");
                entry.SetAttribute("value", "1234567890987654321");
                node.AppendChild(entry);
                //save it   
                XmlTextWriter writer = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/web.config"), null);
                writer.Formatting = Formatting.Indented;
                cfgDoc.WriteTo(writer);
                writer.Flush();
                writer.Close();
                //ScriptManager.RegisterStartupScript(AjaxPanel, this.GetType(), "updateScript", "alert('添加成功')", true);
            }
        }
        catch
        {
            //ScriptManager.RegisterStartupScript(AjaxPanel, this.GetType(), "updateScript", "alert('操作异常，添加失败！')", true);
        }
        DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Content/Upload/"));
        FileSystemInfo[] fsi = di.GetFileSystemInfos();
        if (fsi.Length > 0)
        {
            foreach (var t in fsi)
            {
                _Files.Text += t.FullName + "<br />";
            }
        }
    }
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUp.HasFile)
        {
            string filename = System.Web.HttpContext.Current.Server.MapPath("~/Content/Upload/") + FileUp.FileName;
            FileUp.SaveAs(filename);
            uploadState.Text = "文件已上传至：" + filename;
            string args = "Rar.exe x " + filename + " " + System.Web.HttpContext.Current.Server.MapPath("~/Content/UnpackTemp/") + FileUp.FileName;
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = RarPath;
            psi.Arguments = args;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
            uploadState.Text += "<br />" + "解压完成.";
        }
        else
        {
            uploadState.Text = "未选择文件";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        OpenDBT();
    }
    public void OpenDBT()
    {
        SqlConnection conn = new SqlConnection("Data Source=172.16.0.17;Initial Catalog=JMSNEWRWDB;Persist Security Info=True;User ID=sa;Password=lddq;");
        Stopwatch sw = Stopwatch.StartNew();
        sw.Start();
        try
        {
            SqlCommand cmd = new SqlCommand("WAITFOR DELAY ’00:01’", conn);
            cmd.CommandTimeout = 1;
            conn.OpenDb();
            cmd.ExecuteNonQuery();
            Wait.Text = " Succeeded.用时：" + sw.Elapsed;
        }
        catch
        {
            Wait.Text = " Failure.用时：" + sw.Elapsed;
        }
        finally
        {
            sw.Stop();
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
    }
    protected void EXECSQL_Click(object sender, EventArgs e)
    {
        if (SQLUp.HasFile)
        {
            string filename = System.Web.HttpContext.Current.Server.MapPath("~/Content/Upload/") + SQLUp.FileName;
            SQLUp.SaveAs(filename);
            string args = String.Format(" -U {0} -P {1} -S {2} -i {3}", "sa", "lddq", ".", filename);
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "osql.exe";
            psi.Arguments = args;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "success", "alert('" + filename + " 添加成功')", true);
        }
    }
    protected void MainSend_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("lddq_oj@163.com", "lddq_oj@163.com", "System.Net.Main.MailMessage", DateTime.Now.ToString() + EmailContent.Text);
        mail.Priority = System.Net.Mail.MailPriority.High;
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.163.com", 25);
        client.Credentials = new System.Net.NetworkCredential("lddq_oj", "luacloud321");
        client.Send(mail);
    }
}